﻿// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System.Reflection;
using Autofac;
using Byond.Frontend.Controls;
using Byond.Frontend.EnsembleBrowserNew;
using Byond.Frontend.EnsembleBrowserNew.ViewModels;
using Byond.Frontend.OnlineDebugging.ViewModels;
using Byond.Frontend.ViewModels;
using Byond.Frontend.Views;
using Mvvm;
using Mvvm.Bindings;
using Utils.DialogManagement;

namespace Byond.Frontend
{
	public abstract class FrontendBootstrapper : FrameworkBootstrapper
	{
		protected FrontendBootstrapper()
		{
			// Byond.Frontend
			RegisterAssemblyTypes(typeof(FrontendBootstrapper).Assembly);

			ContainerBuilder.RegisterViewBindings(
				new SimplePropertyToViewBinding<AvalonEditor>(AvalonEditor.TextProperty));

			ContainerBuilder.RegisterType<EnsembleAdministrationWorkflows>().AsSelf().SingleInstance();
			ContainerBuilder.RegisterType<EnsembleRunnerWorkflow>().As<IEnsembleRunner>().ExternallyOwned().SingleInstance();
			ContainerBuilder.RegisterType<DialogManager>().As<IDialogManager>().SingleInstance();
			ContainerBuilder.RegisterType<ByondControl>().FindConstructorsWith(BindingFlags.NonPublic).As<IDialogHost>().AsSelf().SingleInstance();
			ContainerBuilder.Register(c => c.Resolve<ByondControl>().Dispatcher);
			ContainerBuilder.RegisterType<ObjectInspectorViewModel>().SingleInstance();

			ContainerBuilder.RegisterType<CodeEditorViewModel>().SingleInstance();
			ContainerBuilder.RegisterType<CodeEditorView>().SingleInstance();

			ContainerBuilder.RegisterType<LogViewModel>().SingleInstance();
			ContainerBuilder.RegisterType<LogView>().SingleInstance();
			ContainerBuilder.RegisterType<LogMessageCache>().SingleInstance();

			ContainerBuilder.RegisterType<LiveViewModel>().InstancePerDependency();
			ContainerBuilder.RegisterType<LiveView>().InstancePerDependency();

			ContainerBuilder.RegisterType<BrowserViewModel>().AsSelf().SingleInstance();
			ContainerBuilder.RegisterType<MenuItemRegistrar>().AsSelf().SingleInstance();

			ContainerBuilder.RegisterType<DocumentManager>().AsSelf().As<IDocumentManager>().SingleInstance();
			ContainerBuilder.RegisterType<ItemSelectionArbiter>().AsSelf().SingleInstance();
		}

		protected override void ContainerBuilt()
		{
			Container.Resolve<LogMessageCache>();
		}

		protected void InitializeLibrary()
		{
			Container.Resolve<LibraryFactoryResetWorkflow>().ResetToFactoryDefaultsIfEmpty();
		}
	}
}
